<?php
/*
 * page to allow admin staff to manage timesheet periods
*/
//check for an admin session
include("../inc_files/utils/checkadmin.php");
//include the page header
include("../inc_files/ui/header.php");?>
<title>Manage timesheet periods</title>
<script type="text/javascript" src="../scripts/async.js"></script>
<script type='text/javascript'>
//async refresh of timesheet periods datagrid
function processddl(){
	var ddl = document.getElementById('tsperiodyear');
	if(ddl.selectedIndex == 0){
		document.getElementById('datagrid').innerHTML = "";
		document.getElementById('submitlabel').innerHTML = "";
		document.getElementById('submitbutton').style.visibility = "hidden"; 		
	} else if(ddl.selectedIndex == ddl.length -1) {		
		var selection = ddl.options[ddl.selectedIndex].value;
		if(confirm("Timesheet periods have not been created for " + selection + ".  Would you like to create them now?")){
			/*-------------
			/*create timesheet periods for the selected new year
			---------------
			*/
			//display the busy animation
			document.getElementById('datagrid').innerHTML = "<div style='width: 100%; padding-top: 50px;' align='center' ><img src='../images/ajax-loader.gif' alt='Please wait, new records being generated.' height='120' width='120' ></div>";
			//post the data
			postasync('a_createtimesheetperiods.php', 'value', 'year='+selection, false);
			//modify the drop down list
			ddl.options[ddl.selectedIndex].text = selection;
			var newoption = document.createElement("option");
			ddl.options.add(newoption);
			var nextyear = parseInt(selection)+1;
			newoption.text = nextyear + " - New";
			newoption.value = nextyear;
			//refresh the grid
			refreshgrid();		    
		    
		}
	}  else {
		refreshgrid();
	}		
}

function refreshgrid(ddl){
	var ddl = document.getElementById('tsperiodyear');
	var selectedYear = ddl.options[ddl.selectedIndex].value;
	var nochache = Math.floor(Math.random() * 10000);
	getasync('a_listtimesheetperiods.php?&nocache=' + nochache + '&year=' + selectedYear,'datagrid', true);
	document.getElementById('submitlabel').innerHTML = "Submit changes to timesheet periods for " + selectedYear;
	document.getElementById('submitbutton').style.visibility = "visible";
	
}


function submitchanges(){
	var el = document.getElementById('tspform').elements;
	var formdata = "";
    for(var i = 0; i < el.length; i++)
    {
		if(el[i].type == 'select-one'){
            
			formdata += el[i].id + "~" + el[i].options[el[i].selectedIndex].value + "|";
			
        }

    }
    
    //remove the last trailing &
    formdata = 'formdata=' + formdata.slice(0,-1);
    //submit the changes
    postasync('a_updateperiods.php', 'value', formdata, false);    
	alert("Records updated");
	document.getElementById('submitbutton').disabled = false;
}

function changeDayBackground(ddl){

	var newCSSClass = 'day'+ddl.options[ddl.selectedIndex].value+'Cell day';
	ddl.parentNode.className = newCSSClass;
		
}

function changeWeekBackground(ddl){
if(ddl.options[ddl.selectedIndex].value == 1){
	ddl.parentNode.style.backgroundColor = '#FFB2B2';
	
} else {
	ddl.parentNode.style.backgroundColor = 'transparent';
}
	
	
		
}
</script>

</head>
<body>
	<script type="text/javascript" src="../scripts/datepicker.js"></script>
	<?php 	include("../inc_files/ui/menu.php");?>
	<form action="addproject.php" method="post" id="tspform">
		<table style="width: 100%; align: center">
			<tr>
				<td style="width: 100%; font-size: 10pt; color: black;"
					align="center">Please select a year:&nbsp;&nbsp; <?php
					echo "<select name=\"tsperiodyear\" id=\"tsperiodyear\" onchange=\"processddl(this)\">";

					/*this include file creates the body
					 *  (<option value=".... bit) of a drop down list
					*  showing all timesheet years
					*/
					echo "<option value=\"Please select...\">Please select...</option>";
					//create drop down list options only for years which have timesheet periods created

					// Create connection
					include("../inc_files/utils/dbconnection.php");
					//query database
					$query ="SELECT DISTINCT LEFT(PeriodID,4) AS 'Year' FROM timesheet_periods;";
					$result = $mysqli->query( $query );
					//fill the array
					$year=0;
					while($row = mysqli_fetch_array($result))
					{
						$year = $row['Year'] ;
						echo "<option value=\"" . $year ."\">" . $year . "</option>";

					}
					$year ++;
					//if there are no previous records
					if($year == 1) $year = date("Y");
					echo "<option value=\"" . $year ."\">" . $year . " - New</option>";
					//tidy up database connection
					$mysqli->close();


					echo "</select>";
					?>

				</td>
			</tr>
			<tr>
				<td style="width: 100%;" align="center">&nbsp;</td>
			</tr>
			<tr>
				<td align="center" style="position: relative; z-index: 1;">
				<!-- add a div tag to display the timesheet periods using AJAX -->
				<div id="datagrid" 
					style="position: relative; z-index: 2; width: 900px; height: 600px; overflow-x: hidden; overflow-y: scroll; border: 1px solid #E0E0E0;"></div>


			</td>
			</tr>			
			<tr>
				<td style="width: 100%;" align="center">
					<div id="submitlabel"
						style="font-size: 10pt; color: black; font-weight: bold; padding: 30px;"></div>
					<input type="button" value="Submit" id="submitbutton"
					style="visibility: hidden"
					onclick="this.disabled = 'true'; submitchanges();"></input>
				</td>
			</tr>
		</table>
	</form>
	<?php
	//add the page footer
	include("../inc_files/ui/footer.php");?>


</body>
</html>
